<!-- 商品订单打印配置 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form :label-col="{ span: 5 }" :wrapper-col="{ span: 17 }">
                <a-form-item label="所在仓库">
                    <a-select v-model="storehouseId" placeholder="请选择" @change="getData">
                        <a-select-option v-for="(itm) in storeList" :value="itm.storehouseId" :key="itm.storehouseId">{{itm.storehouseName}}</a-select-option>
                    </a-select>       
                </a-form-item>
                <a-form-item label="自动打印">
                    <a-switch checked-children="开启" un-checked-children="关闭" :checked="autoStatus == '1' ? true : false" @change="onChange"/>
                </a-form-item>
                <a-form-item v-bind="formItemLayoutWithOutLabel">
                    <a-button type="primary" @click="submit">确定</a-button>
                </a-form-item>
            </a-form>
        </div>  
    </div>
</template>

<script>
import { queryGoodsOrderPrintConfig , updateGoodsOrderPrintConfig } from '@/api/brand';
import { queryGoodsStorehouseBindConfig } from '@/api/newMall';
export default {
    data () {
        return {
            autoStatus: '1', // 状态
            brandId: this.global.brandId,
            id: '',
            formItemLayoutWithOutLabel: {
                wrapperCol: {
                    xs: { span: 24, offset: 0 },
                    sm: { span: 20, offset: 6 },
                },
            },
            storehouseId: undefined,
            storeList: [],
            userId: sessionStorage.getItem('userId'),
        };
    },
    async created(){
        try {
            await this._queryGoodsStorehouseBindConfig();
            this.getData();
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询用户所包含的仓库信息
        _queryGoodsStorehouseBindConfig(){
            let _this = this;
            return new Promise((resolve,reject) => {
                queryGoodsStorehouseBindConfig({userId: _this.userId,size: 99})
                .then(res => {
                    if(res.success){
                        _this.storeList = res.data.content;
                        _this.storehouseId = res.data.content.length > 0 ? res.data.content[0].storehouseId : '';
                        resolve();
                    }else{
                        reject(res.errMessage);
                    }
                })
            })
        },
        getData(){
            let _this = this;
            const params = {
                page: 0,
                size: 20,
                'storehouseId': _this.storehouseId,
            }
            queryGoodsOrderPrintConfig(params)
            .then(res => {
                if(res.success){
                    if(res.data.content.length > 0){
                        _this.autoStatus = res.data.content[0].autoStatus;
                        _this.id = res.data.content[0].id;
                    }else{
                        _this.autoStatus = '1';
                        _this.id = '';
                    }
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        submit(){
            let _this = this;
            const params = {
                'id': _this.id,
                'autoStatus': _this.autoStatus,
                'brandId': _this.brandId,
                'storehouseId': _this.storehouseId,
            }
            updateGoodsOrderPrintConfig(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        onChange(checked) {
            console.log(`a-switch to ${checked}`);
            this.autoStatus = checked ? '1' : '0'
        },
    }
}

</script>
<style scoped>

</style>